﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RipPoll.Web.Mvc.Models;
using RipPoll.Library;
using RipPoll.Common;

namespace RipPoll.Web.Mvc.Controllers
{
    public class UserController : Controller
    {
        public ActionResult SignIn()
        {
            return View();
        }


        public ActionResult newpopupsignin()
        {
            ViewBag.LayoutModel = null;
            return PartialView("newpopupsignin");
        }


        public ActionResult Signout()
        {
            Session[WebConstants.CurrentUser] = null;
            return RedirectToAction("myrippolls", "poll");
        }


        [HttpPost]
        public ActionResult SignIn(SignInModel model)
        {
            //if (!ModelState.IsValid)
            //    return PartialView("_popupsignin", model);

            string useroremail = model.usernameoremail;
            string password = model.password;

            UserServices.UserServicesClient userClient = new UserServices.UserServicesClient();
            UserPE upe = null;

            if (!string.IsNullOrEmpty(useroremail))
            {
                if (useroremail.IndexOf("@") > 0)
                    upe = userClient.GetUserByEmailId(useroremail);
                else
                    upe = userClient.GetUserByScreenName(useroremail);
            }

            if (upe != null && !string.IsNullOrEmpty(upe.PassWord))
            {
                if (upe.PassWord.Equals(password, StringComparison.Ordinal))
                {
                    Session[WebConstants.CurrentUser] = upe;
                    return RedirectToAction("myrippolls", "poll");
                }
                else
                    TempData[WebConstants.SignInMessage] = "Email or Screen name and password doesn't match";
            }
            else
                TempData[WebConstants.SignInMessage] = "Email or Screen name doesn't exists";

            return View();
        }

        [HttpPost]
        public ActionResult newpopupsignin(SignInModel model)
        {
            ViewBag.LayoutModel = null;

            if (!ModelState.IsValid)
                return Content("Please review the form");

            string useroremail = model.usernameoremail;
            string password = model.password;

            UserServices.UserServicesClient userClient = new UserServices.UserServicesClient();
            UserPE upe = null;

            if (!string.IsNullOrEmpty(useroremail))
            {
                if (useroremail.IndexOf("@") > 0)
                    upe = userClient.GetUserByEmailId(useroremail);
                else
                    upe = userClient.GetUserByScreenName(useroremail);
            }

            if (upe != null && !string.IsNullOrEmpty(upe.PassWord))
            {
                if (upe.PassWord.Equals(password, StringComparison.Ordinal))
                {
                    Session[WebConstants.CurrentUser] = upe;
                    return Content(Boolean.TrueString);
                }
                else
                    return Content("Email or Screen name and password doesn't match");
            }
            else
                return Content("Email or Screen name doesn't exists");

        }

        public ActionResult newpopupjoin()
        {
            ViewBag.LayoutModel = null;
            UserModel um = new UserModel();
            um.EducationBackgrounds = GetEducationBackgrounds();
            return PartialView("newpopupjoin", um);
        }

        public ActionResult Join()
        {
            UserModel um = new UserModel();
            um.EducationBackgrounds = GetEducationBackgrounds();
            return View(um);
        }

        private List<EducationNamesPE> GetEducationBackgrounds()
        {
            UserServices.UserServicesClient userClient = new UserServices.UserServicesClient();
            return userClient.GetEducationNames().ToList<EducationNamesPE>();

        }


        [HttpPost]
        public ActionResult newpopupjoin(UserModel model)
        {
            ViewBag.LayoutModel = null;

            if (!ModelState.IsValid)
                return Content("Please review the form");

            UserServices.UserServicesClient usvc = new UserServices.UserServicesClient();

            bool screenExists = false;
            bool emailExists = usvc.CheckUserForEmailIdAndScreenName(out screenExists, model.Email, model.ScreenName);
            if (emailExists)
                return Content("Email is already in use...");
            if (screenExists)
                return Content("Screen Name is already in use...");

            UserPE upe = new UserPE();
            upe.BirthYear = model.BirthYear;
            upe.City = model.City;
            upe.DateAdded = DateTime.Now;
            upe.DateUpdated = null;
            
            upe.Email = model.Email;
            upe.Gender = model.SelectedGender;
            upe.PassWord = model.PassWord;
            upe.ScreenName = model.ScreenName;
            upe.ZipCode = model.ZipCode;
            upe.EducationId = model.SelectedEducationId;

            usvc.AddUser(upe);

            Session[WebConstants.CurrentUser] = upe;
            return Content(Boolean.TrueString);
        }

        [HttpPost]
        public ActionResult Join(UserModel model, string returnUrl)
        {

            if (!ModelState.IsValid)
            {
                model.EducationBackgrounds = GetEducationBackgrounds();
                return View(model);
            }

            UserServices.UserServicesClient usvc = new UserServices.UserServicesClient();

            bool screenExists = false;
            bool emailExists = usvc.CheckUserForEmailIdAndScreenName(out screenExists, model.Email, model.ScreenName);
            if (emailExists)
            {
                ModelState.AddModelError("", "Email is already in use...");
                model.EducationBackgrounds = GetEducationBackgrounds();
                return View(model);
            }
            if (screenExists)
            {
                ModelState.AddModelError("", "Screen Name is already in use...");
                model.EducationBackgrounds = GetEducationBackgrounds();
                return View(model);
            }

            UserPE upe = new UserPE();
            upe.BirthYear = model.BirthYear;
            upe.City = model.City;
            upe.DateAdded = DateTime.Now;
            upe.DateUpdated = null;
            //upe.EducationId = model.ed
            upe.Email = model.Email;
            upe.Gender = model.SelectedGender;
            upe.PassWord = model.PassWord;
            upe.ScreenName = model.ScreenName;
            upe.ZipCode = model.ZipCode;
            upe.EducationId = model.SelectedEducationId;

            usvc.AddUser(upe);

            Session[WebConstants.CurrentUser] = upe;
            TempData.Add(WebConstants.StatusMessage, "User Suceessfully Created...");
            return RedirectToAction("join", "user");
        }

        
    }
}
